from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from app01.ser import *
from app01.models import *
from django.core.paginator import Paginator


class CateAPI(APIView):
    def get(self, request):
        data = Cate.objects.all()
        all_data = CateModelSerializer(data, many=True)
        return Response(all_data.data)


class BookAPI(APIView):
    def get(self, request):
        id = request.GET.get('id')
        pid = request.GET.get('fid')
        if pid:
            ser = Books.objects.filter(cate=id)
            page = Paginator(ser, 2)
            page1 = page.page(pid)
            all_data = ShowBookSerializer(page1, many=True)
            return Response({
                'data': all_data.data,
                'page_range': [i for i in page.page_range]
            })
        else:
            data = Books.objects.all()
            all_data = ShowBookSerializer(data, many=True)
            return Response(all_data.data)


class OrderAPI(APIView):
    def get(self, request):
        cate = request.GET.get('cate')
        if type == '1':
            data = Books.objects.order_by('-' + 'price')
            all_data = ShowBookSerializer(data, many=True)
            return Response(all_data.data)
        else:
            data = Books.objects.order_by('price')
            all_data = ShowBookSerializer(data, many=True)
            return Response(all_data.data)


class SousuoAPI(APIView):
    def get(self, request):
        meg = request.GET.get('meg')
        data = Books.objects.filter(name__contains=meg).all()
        all_data = BooksModelSerializer(data, many=True)
        return Response(all_data.data)
